
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * Created by abely on 16-5-17.
 */
public class GithubDemo {
    public static void main(String[] args) throws IOException {
        HttpClientBuilder clientBuilder = HttpClientBuilder.create();
        HttpHost proxyTarget = new HttpHost("172.26.100.238",64000);
        clientBuilder.setProxy(proxyTarget);
        Credentials defaultcreds = new UsernamePasswordCredentials("abely_liu", "Welcome5");
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY, defaultcreds);
        clientBuilder.setDefaultCredentialsProvider(credsProvider);
        CloseableHttpClient httpClient = clientBuilder.build();
        String url ="https://github.com/search?l=&o=desc&q=language%3AJava&ref=advsearch&s=stars&type=Repositories&utf8=%E2%9C%93";
        HttpGet httpGet = new HttpGet(url);
        String firstPage = EntityUtils.toString(httpClient.execute(httpGet).getEntity());
        Elements elements = Jsoup.parse(firstPage).body().select(".repo-list-stats");
//        System.out.println(elements.size());
        int i=0;
        for(Element element:elements){
            String s = element.child(0).text();
            String s2 = element.child(1).text();
            String s3 = Jsoup.parse(firstPage).body().select(".repo-list-name").get(i).text();

            s=StringUtils.replace(s,",","");
            s2= StringUtils.replace(s2,",","");
            float value = Integer.parseInt(s)/Float.parseFloat(s2);
            System.out.println(s3+":"+value);
            i++;
        }

    }
}
